package icom.icss.register;

import icom.icss.entity.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

public class JDBCServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");

            request.setCharacterEncoding("UTF-8");
//            get方法不用  post用。  处理中文乱码问题
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        UserServive userServive = new UserServive();

        try {
            userServive.register(user);
//            response.sendRedirect("log_in.jsp");
              response.setStatus(302);
              response.setHeader("location","log_in.jsp");
        } catch (SQLException e) {
            e.printStackTrace();
             request.setAttribute("registerInfo","注册失败");
             request.getRequestDispatcher("sign_in.jsp").forward(request,response);
        }




    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       doPost(request,response);

    }
}
